// 表单校验规则由 schema2code 生成，不建议直接修改校验规则，而建议通过 schema2code 生成, 详情: https://uniapp.dcloud.net.cn/uniCloud/schema

const validator = {
	name: {
		rules: [
			{
				required: true
			},
			{
				format: 'string'
			}
		],
		label: '名称',
		title: '名称'
	},
	nickname: {
		rules: [
			{
				format: 'string'
			}
		],
		label: '昵称',
		title: '昵称'
	},
	description: {
		rules: [
			{
				format: 'string'
			}
		],
		label: '描述',
		title: '描述'
	},
	tabs: {
		rules: [
			{
				required: true
			},
			{
				format: 'string'
			}
		],
		label: '标签',
		title: '标签'
	},
	class_id: {
		rules: [
			{
				required: true
			},
			{
				format: 'string'
			}
		],
		title: '分类',
		label: '分类'
	},
	score: {
		rules: [
			{
				required: true
			},
			{
				format: 'double'
			}
		],
		label: '系统评分',
		title: '系统评分',
		defaultValue: 0
	},
	sort: {
		rules: [
			{
				format: 'int'
			}
		],
		label: '排序',
		title: '排序'
	},
	view_count: {
		rules: [
			{
				format: 'int'
			}
		],
		label: '浏览量',
		title: '浏览量'
	},
	select: {
		rules: [
			{
				required: true
			},
			{
				format: 'bool'
			}
		],
		title: '是否推荐',
		defaultValue: false,
		label: '是否推荐'
	},
	picurl: {
		rules: [
			{
				required: true
			},
			{
				format: 'file'
			}
		],
		label: '图片',
		title: '图片'
	},
	del_status: {
		rules: [
			{
				format: 'bool'
			}
		],
		title: '是否删除',
		label: '是否删除'
	}
};

const enumConverter = {};

function filterToWhere(filter, command) {
	let where = {};
	for (let field in filter) {
		let { type, value } = filter[field];
		switch (type) {
			case 'search':
				if (typeof value === 'string' && value.length) {
					where[field] = new RegExp(value);
				}
				break;
			case 'select':
				if (value.length) {
					let selectValue = [];
					for (let s of value) {
						selectValue.push(command.eq(s));
					}
					where[field] = command.or(selectValue);
				}
				break;
			case 'range':
				if (value.length) {
					let gt = value[0];
					let lt = value[1];
					where[field] = command.and([command.gte(gt), command.lte(lt)]);
				}
				break;
			case 'date':
				if (value.length) {
					let [s, e] = value;
					let startDate = new Date(s);
					let endDate = new Date(e);
					where[field] = command.and([command.gte(startDate), command.lte(endDate)]);
				}
				break;
			case 'timestamp':
				if (value.length) {
					let [startDate, endDate] = value;
					where[field] = command.and([command.gte(startDate), command.lte(endDate)]);
				}
				break;
		}
	}
	return where;
}

export { validator, enumConverter, filterToWhere };
